package com.youshang520i.demo.service.impl;

import com.youshang520i.demo.mapper.StockMapper;
import com.youshang520i.demo.entity.Stock;
import com.youshang520i.demo.service.StockService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author youshang
 * @date 2021/03/11 14:16
 **/
@Service
public class StockServiceImpl implements StockService {
    @Autowired
    private StockMapper stockMapper;

    @Transactional
    @Override
    public void deductions() {
        Stock stock = stockMapper.get();
//        if (stock.getStock() > 0){
//            System.out.println("准备进入循环。。。。。。" + stock + "     "+ Thread.currentThread().getName());
        stockMapper.update(stock.getVersion());
//            while (stock.getStock() > 0 && stockMapper.update(stock.getVersion()) == 0) {
//                stock = stockMapper.get();
//                System.out.println(Thread.currentThread().getName()+"进入循环。。。。。。" + stock.getVersion());
//            }
//        }
    }

    @Transactional
    @Override
    public void deductions2() {
        Stock stock = stockMapper.get();
        if (stock.getStock() > 0) {
            System.out.println("准备进入循环。。。。。。" + stock + "     " + Thread.currentThread().getName());
            while (stock.getStock() > 0 && stockMapper.update2(stock.getVersion()) == 0) {
                stock = stockMapper.get();
                System.out.println(Thread.currentThread().getName() + "进入循环。。。。。。" + stock.getVersion());
            }
        }
    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @Override
    public void deductions3() {
        Stock stock = stockMapper.get3();
        if (stock.getStock() > 0) {
            System.out.println("准备进入循环。。。。。。" + stock + "     " + Thread.currentThread().getName());
            stockMapper.update(stock.getVersion());
//            while (stock.getStock() > 0 && stockMapper.update(stock.getVersion()) == 0) {
//                stock = stockMapper.get();
//                System.out.println(Thread.currentThread().getName()+"进入循环。。。。。。" + stock.getVersion());
//            }
        }
    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @Override
    public void deductions4() {
        Stock stock = stockMapper.get4();
        if (stock.getStock() > 0) {
            System.out.println("准备进入循环。。。。。。" + stock + "     " + Thread.currentThread().getName());
            while (stock.getStock() > 0 && stockMapper.update2(stock.getVersion()) == 0) {
                stock = stockMapper.get4();
                System.out.println(Thread.currentThread().getName() + "进入循环。。。。。。" + stock.getVersion());
            }
        }


    }


}
